﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using Yugi.Client.Card.Load;
using Yugi.Client.Card;
using Yugi.Client;
using Yugi.Client.Control;
using Yugi_Gui.Animation;
using Yugi.Client.ViewModel;

namespace Yugi_Gui
{
	/// <summary>
	/// Interaction logic for GameLayout.xaml
	/// </summary>
	public partial class GameLayout : UserControl
	{
        public Yugi.Client.SupervisorModel s;
		public GameLayout()
		{
			this.InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
            
		}

        public void GameLayout_Unloaded()
        {
            try
            {
                //s.SendMessageToServer(Common.YMSG.PLAYER_LOGOUT);
                //MainServerHandler mainserver = ((Window)(this.Parent)).DataContext as MainServerHandler;
                //mainserver.SendMessageToServer(Common.MSG_GSM.L_OutTable,mainserver.Current_Table_ID);
                //Thread.Sleep(1000);
                //mainserver.SendMessageToServer(Common.MSG_GSM.L_OutRoom, mainserver.Current_Room_ID);
            }
            catch (Exception ex)
            {
            }
        }


        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                s = new SupervisorModel(2010, this);
                //--------Test-------------------
                //s = new SupervisorModel(this);
                //---------------------------
                s.InitCuaSo = ConnectModelToView;
                PathManager.DataRoot = System.IO.Directory.GetCurrentDirectory() + "\\player";
                HostItem.Presenter = this.root_layout;
              //  Linker.MainWindow = this;
                Linker.Lager_img = this.Large_Image;
                // Init Exp Storyboard Base Animation
                //InitAnimation1();

                // Init Exp Sprite Base Animation
                InitAnimation2();


                this.EffectLayer.Background = Brushes.Black;
                this.EffectLayer.Opacity = 0.9;
                this.EffectLayer.IsHitTestVisible = true;

                //--------Test-------------------
                //LoadTestData();
                //TestBeginGame();
                //---------------------------

                Action ac = new Action(this.Reset);
                this.DataContext = ac;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Skip_Button_Click(object sender, RoutedEventArgs e)
        {
            if (s.AllowSkip)
            {
                s.SendMessageToServer(Common.YMSG.TURN_SKIP);
            }
            else
            {
                //MessageBox.Show("Ban Khong Co Luot");
            }
        }
        /// <summary>
        /// No Binh Thuong
        /// </summary>
        private void InitAnimation1()
        {
            SwordFlyAnimation sw = new SwordFlyAnimation(this.sw_image, this.Resources["KiemBay"] as Storyboard);
            sw.From = new Point(0, 0);
            sw.To = new Point(150, 150);
            Explosive_Aniamtion exp = new Explosive_Aniamtion(this.exp, this.Resources["No"] as Storyboard);
            exp.Center = new Point(150, 150);

            PlayStoryBoardSchedue st1 = new Yugi_Gui.Animation.PlayStoryBoardSchedue(this.Resources["KiemBay"] as Storyboard);
            PlayStoryBoardSchedue st2 = new Yugi_Gui.Animation.PlayStoryBoardSchedue(this.Resources["No"] as Storyboard);
            AnimationSchedue sche = new AnimationSchedue(Linker.CompleteBattleSchedue, st1, st2);


            Linker.BattleSchedue = sche;
            Linker.sw_fly = sw;


        }

        /// <summary>
        /// No Bang Sprite
        /// </summary>
        private void InitAnimation2()
        {
            SwordFlyAnimation sw = new SwordFlyAnimation(this.sw_image, this.Resources["KiemBay"] as Storyboard);
            sw.From = new Point(0, 0);
            sw.To = new Point(150, 150);
            //Explosive
            this.sq_effect.LoadBuffer(11, "pack://application:,,,/Image/Particles/Exp/explosion{0}.png");
            this.sq_effect.TickChange = 100;
            Explosive_Aniamtion_Sprite exp = new Explosive_Aniamtion_Sprite(this.sq_effect);
            exp.Center = new Point(150, 150);
            //Transport
            this.sq_effect_transport.LoadBuffer(47, "pack://application:,,,/Image/Particles/Transport/active{0}.png");
            this.sq_effect_transport.TickChange = 30;
            TransportAnimation trans = new TransportAnimation(this.sq_effect_transport);
            trans.Center = new Point(((Window)this.Parent).ActualWidth / 2 + 26, ((Window)this.Parent).ActualHeight / 2 - 30);

            // Explosive Chain
            PlayStoryBoardSchedue st1 = new Yugi_Gui.Animation.PlayStoryBoardSchedue(this.Resources["KiemBay"] as Storyboard);
            SpriteSchedue st2 = new SpriteSchedue(this.sq_effect);
            AnimationSchedue sche = new AnimationSchedue(Linker.CompleteBattleSchedue, st1, st2);

            // Active Schedue
            InvokeSchedue invokBegin = new InvokeSchedue(() =>
            {
                this.Large_Image.Visibility = Visibility.Visible;
            });
            SpriteSchedue sp_transport = new SpriteSchedue(this.sq_effect_transport);
            InvokeSchedue invokEnd = new InvokeSchedue(() =>
            {
                this.Large_Image.Visibility = Visibility.Collapsed;
            });
            AnimationSchedue active_sche = new AnimationSchedue(Linker.CompleteActiveSchedue, invokBegin, sp_transport, invokEnd);


            Linker.ActiveSchedue = active_sche;
            Linker.BattleSchedue = sche;
            Linker.sw_fly = sw;
            Linker.explosive = exp;
            Linker.transport = trans;

        }
        /// <summary>
        /// Begin Game
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_begin_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            int id = 0;
            if( (((Window)(this.Parent)).DataContext as MainServerHandler)!=null)
                id = (((Window)(this.Parent)).DataContext as MainServerHandler).External_Model.UserID;
            s.SendMessageToServer(Common.YMSG.PLAYER_LOGIN,id);
            this.bt_begin.Click -= new RoutedEventHandler(bt_begin_Click);
            //((Canvas)this.Content).Children.Remove(bt_begin);
            bt_begin.Visibility = Visibility.Collapsed;
            this.EffectLayer.Background = Brushes.Transparent;
            this.EffectLayer.Opacity = 1;
            this.EffectLayer.IsHitTestVisible = false;
        }
        
        public void Reset()
        {
            this.bt_begin.Click += new RoutedEventHandler(bt_begin_Click);
            bt_begin.Visibility = Visibility.Visible;
            this.EffectLayer.Background = Brushes.Black;
            this.EffectLayer.Opacity = 0.8;
            this.EffectLayer.IsHitTestVisible = true;
        }
        //------------------------------------------Test Message---------------------------------//
        #region Test

        private void LoadTestData()
        {
            // PathManager.DataRoot = @"C:\Users\HauVo\Desktop\YuGi_Group\ServerYuGi\bin\Debug\player";
            LoadCardData load = new LoadCardData();
            LoadManager manager = new LoadManager();
            manager.Add(load);
            manager.Load(null);
        }
        private void ConnectModelToView(Yugi.Client.SupervisorModel s)
        {
            control_list_card.DataContext = s.Nguoi_Choi;
            control_detail_img.DataContext = s.Viewdetail;
            control_detail_text.DataContext = s.Viewdetail;
            control_table.DataContext = s.TableGame;
            control_turn.DataContext = s.Turn;
            control_health_p0.DataContext = s.Nguoi_Choi;
            control_health_p1.DataContext = s.Doi_Thu;
            control_tomb_p0.DataContext = s.Mo_Nguoi_Choi;
            control_tomb_p1.DataContext = s.Mo_Doi_Thu;
            control_list_card_doithu.DataContext = s.Doi_Thu;
            control_deck.DataContext = s.Nguoi_Choi;
            control_deck_doithu.DataContext = s.Doi_Thu;
        }

        private void TestBeginGame()
        {

            s.ReciveMessageFromServer(Common.YMSG.GAME_BEGIN, 0, 1);

            ConnectModelToView(s);

            //CardManager manager=new CardManager();


            byte[] Buff = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };


            s.ReciveMessageFromServer(Common.YMSG.GAME_CHIABAI, Buff);

        }

        private void TestDrawPharse()
        {
            CardManager manager = new CardManager();
            int i = manager.GetRandom(5)[0];
            s.ReciveMessageFromServer(Common.YMSG.CARD_RUTBAI, new byte[] { (byte)manager[i].Card_TypeID, (byte)manager[i].ID });
        }
        private void TestMF1()
        {
            s.ReciveMessageFromServer(Common.YMSG.TURN_MAINPHASE1_BEGIN, 0);
        }
        private void TestLenBaiHopLe()
        {
            s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_HOPLE, new byte[] { (byte)s.Nguoi_Choi.YListCard[1].YCard.ID, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_MO });
            //s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_HOPLE, new byte[] { (byte)s.Nguoi_Choi.YListCard[0].YCard.ID, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_MO });
            //s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_HOPLE, new byte[] { (byte)s.Nguoi_Choi.YListCard[0].YCard.ID, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_MO });

        }
        private void TestLenBaiDoiThu()
        {
            //id1 = s.Nguoi_Choi.YListCard[0].YCard.ID;
            //s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_XONG, new byte[] { (byte)s.Nguoi_Choi.YListCard[0].YCard.Card_TypeID, (byte)s.Nguoi_Choi.YListCard[0].YCard.ID, (byte)3, (byte)3, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_MO });
            id2 = s.Nguoi_Choi.YListCard[1].YCard.ID;
            s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_XONG, new byte[] { (byte)s.Nguoi_Choi.YListCard[1].YCard.Card_TypeID, (byte)s.Nguoi_Choi.YListCard[1].YCard.ID, (byte)3, (byte)1, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_MO });

        }
        private void TestLenBaiDacBietDoiThu()
        {
            s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_SUMMONDACBIET_XONG, new byte[] { (byte)s.Nguoi_Choi.YListCard[2].YCard.Card_TypeID, (byte)s.Nguoi_Choi.YListCard[2].YCard.ID, 1, (byte)id1, 3, 3, (byte)Common.YMSG.CARD_TRANGTHAI_NAM_MO });
        }
        private void TestDoiCongThuDoiThu()
        {
            s.ReciveMessageFromServer(Common.YMSG.CARD_DOITRANGTHAI_XONG, new byte[] { (byte)s.Nguoi_Choi.YListCard[2].YCard.ID, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_MO });
            //s.ReciveMessageFromServer(Common.YMSG.CARD_DOITRANGTHAI_XONG, s.Nguoi_Choi.YListCard[0].YCard, Common.YMSG.CARD_TRANGTHAI_DUNG_MO);
        }
        private void TestLenBaiDacBietHopLe()
        {
            s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_SUMMONDACBIET_HOPLE, new byte[] { 4 });
        }
        private void TestDoiDiem()
        {
            List<byte> par = new List<byte>();
            par.Add(0);
            par.AddRange(Common.Encoder.Encode(5000));
            s.ReciveMessageFromServer(Common.YMSG.PLAYER_THAYDOIDIEM, par.ToArray());
        }
        private void TestEndGame()
        {
            s.ReciveMessageFromServer(Common.YMSG.GAME_END, 0, 1);
        }
        private void TestThayDoiCongThu()
        {
            List<byte> par = new List<byte>();
            par.Add(1);
            par.AddRange(Common.Encoder.Encode(5000));
            s.ReciveMessageFromServer(Common.YMSG.CARD_SETTHU, par.ToArray());
        }
        private void TestRequest()
        {
            List<byte> par = new List<byte>();
            par.Add(1);
            par.Add(3);
            s.ReciveMessageFromServer(Common.YMSG.REQUEST_CHONBAI_NGUOICHOI_BAN, par.ToArray());
        }
        private void TestChonBaiTayNguoiChoi()
        {
            //CardChoiceHandNguoiChoi choice = new CardChoiceHandNguoiChoi();
            //choice.Data = this.control_list_card.Data;
            //choice.Count = 1;
            //choice.OnChoiceComplete += new EventHandler(choice_OnChoiceComplete);
            //choice.OnError += new Action<object, string>(choice_OnError);
            //choice.Begin();
            List<byte> par = new List<byte>();
            par.Add(1);
            par.Add(2);
            s.ReciveMessageFromServer(Common.YMSG.REQUEST_CHONBAI_NGUOICHOI_TAY, par.ToArray());
        }
        private void TestChonBaiTayDoiThu()
        {
            //CardChoiceHandDoiThu choice = new CardChoiceHandDoiThu();
            //choice.Data = this.control_list_card_doithu.Data;
            //choice.Count = 2;
            //choice.OnChoiceComplete += new EventHandler(choice_OnChoiceComplete);
            //choice.OnError += new Action<object, string>(choice_OnError);
            //choice.Begin();
            List<byte> par = new List<byte>();
            par.Add(1);
            par.Add(2);
            s.ReciveMessageFromServer(Common.YMSG.REQUEST_CHONBAI_DOITHU_TAY, par.ToArray());
        }
        private void TestChangeControl()
        {
            s.ReciveMessageFromServer(Common.YMSG.API_CHANGECONTROL, new byte[] { (byte)1, (byte)2, (byte)0 });
        }
        private void TestLenBaiSpellHopLe()
        {
            s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_SPELL_HOPLE, new byte[] { (byte)s.Nguoi_Choi.YListCard[0].YCard.ID, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_UP });


        }
        private void TestLenBaiSpellDoiThu()
        {
            id1 = s.Nguoi_Choi.YListCard[0].YCard.ID;
            s.ReciveMessageFromServer(Common.YMSG.CARD_LENBAI_XONG, new byte[] { (byte)s.Nguoi_Choi.YListCard[0].YCard.Card_TypeID, (byte)s.Nguoi_Choi.YListCard[0].YCard.ID, (byte)4, (byte)3, (byte)Common.YMSG.CARD_TRANGTHAI_DUNG_UP });


        }
        void choice_OnError(object arg1, string arg2)
        {
            MessageBox.Show(arg2);
        }

        void choice_OnChoiceComplete(object sender, EventArgs e)
        {
            MessageBox.Show("Complete");
        }
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Button b = sender as Button;
            switch (b.Content.ToString())
            {
                case "Test_Begin":
                    //TestLenBaiDoiThu();
                    TestLenBaiHopLe();
                    break;
                case "Test":
                    this.Large_Image.Visibility = Visibility.Visible;
                    this.Large_Image.Source = new BitmapImage(new Uri("pack://application:,,,/COntrol/BackMaterial.jpg"));
                    this.sq_effect_transport.Begin();
                    break;
                case "Test_End":
                    TestChangeControl();
                    break;
            }
        }

        #endregion



        int id1, id2;

	}
}